package com.rdc.example.controller;

import com.rdc.example.bean.dto.ArticleDTO;
import com.rdc.example.bean.dto.ColumnDTO;
import com.rdc.example.bean.dto.CommentDTO;
import com.rdc.example.bean.dto.UserDTO;

import com.rdc.example.view.UserView;
import com.rdc.example.view.UtilView;

import java.util.ArrayList;
import java.util.List;



/**
 *
 * @author duck
 */
public class MainController {
    private static final UtilView utilView = new UtilView();
    private static final UserView userView = new UserView();
    public static final UserController userController = new UserController();

    public static UserDTO loggedInuserDTO = new UserDTO();
    public static UserDTO currentUserDTO = new UserDTO();
    public static List<UserDTO> currentUserDTOList=new ArrayList<>();
    public static List<ArticleDTO> currentArticleDTOList=new ArrayList<>();
    public static ArticleDTO currentArticleDTO = new ArticleDTO();
    public static List<CommentDTO>currentCommentDTOList=new ArrayList<>();
    public static ColumnDTO currentColumnDTO = new ColumnDTO();
    public static List<ColumnDTO>currentColumnDTOList=new ArrayList<>();
    public static boolean isLogIn = false;

    public static void main(String[] args) throws Exception {
       while(true) {
            if (isLogIn) {
                //登录进去的
                showUserMenu();
            } else {
                //未登录的
                showMainMenu();
            }
        }
    }

    public static void showMainMenu() throws Exception {
        userView.showMainMenu();
        String choice = UtilView.getUserInputString("请输入：");
        switch (choice) {
            case "1":
                //登录
                UserController.showLoginMenu();
                break;
            case "2":
                //注册
                UserController.showRegisterMenu();
                break;
            case "3":
                utilView.showMessage("已退出，欢迎下次使用");
                System.exit(0);
            default:
                utilView.showMessage("无效选项！");
        }
    }

    public static void showUserMenu() throws Exception {
        while(isLogIn) {
            currentUserDTO=loggedInuserDTO;
            String choice = UtilView.showUserMenu();
            switch (choice) {
                case "1":
                    showUserSpace();
                    break;
                case "2":
                    myListArticleOperate();
                    break;
                case "3":
                    listColumnOperate();
                    break;
                case "4":
                    showSearchMenu();
                    break;
                case "5":
                    showHotAndNewMenu();
                    break;
                case "9":
                    // 退出登录
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    break;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }

    private  static void showUserSpace() throws Exception {
        while(isLogIn) {
            String choice = UtilView.showUserSpace();
            switch (choice) {
                case "1":
                    UserController.showUserMessage(loggedInuserDTO);
                    break;
                case "2":
                    LikeController.showLikeArticleList(loggedInuserDTO.getId());
                    break;
                case "3":
                    FollowController.showListFollowByUserId(loggedInuserDTO.getId());
                    userOperate();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }

    private static void showHotAndNewMenu() throws Exception {
        while (isLogIn) {
            String choice = UtilView.showHotAndNewMenu();
            switch (choice) {
                case "1":
                    ArticleController.showListArticleSortByUpdateAtDesc();
                    listArticleOperate();
                    break;
                case "2":
                    ArticleController.showListArticleSortByLikesDesc();
                    listArticleOperate();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");

            }
        }
    }

    private static void myListArticleOperate() throws Exception {
        ArticleController.showListArticleByUserId(currentUserDTO.getId());
        while (isLogIn) {
            String choice = UtilView.myListArticleOperate();
            switch (choice) {
                case "1":
                    ArticleController.insertArticle(loggedInuserDTO.getId());
                    articleOperate();
                    break;
                case "2":
                    ArticleController.updateArticle(currentArticleDTOList);
                    articleOperate();
                    break;
                case "3":
                    ArticleController.deleteArticle(currentArticleDTOList,loggedInuserDTO);
                    break;
                case "4":
                    articleOperate();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");

            }
        }
    }

    private static void listArticleOperate() throws Exception {
        while (isLogIn&& !currentArticleDTOList.isEmpty()) {
            String choice = UtilView.listArticleOperate();
            switch (choice) {
                case "1":
                    articleOperate();
                    break;
                case "2":
                    ArticleController.deleteArticle(currentArticleDTOList,loggedInuserDTO);
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");

            }
        }
    }


    private static void articleOperate() throws Exception {
        ArticleController.turnToArticle(currentArticleDTOList);
        while (isLogIn) {
            String choice = UtilView.articleOperate();
            switch (choice) {
                case "1":
                    CommentController.insertComment(loggedInuserDTO.getId(),currentArticleDTO.getId());
                    break;
                case "2":
                    CommentController.deleteComment(loggedInuserDTO,currentArticleDTO,currentCommentDTOList);
                    break;
                case "3":
                    LikeController.turnLike(loggedInuserDTO.getId(), currentArticleDTO.getId());
                    break;
                case "4":
                    FollowController.turnFollowerId(loggedInuserDTO.getId(), currentArticleDTO.getUserId());
                    break;
                case "5":
                    CommentController.turnCommentLike(currentCommentDTOList,loggedInuserDTO.getId());
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            } ArticleController.showArticleAndComment(currentArticleDTO,currentCommentDTOList);
        }
    }

    private static void listColumnOperate() {
        ColumnController.showListColumnByUserId(currentUserDTO.getId());
        while (isLogIn) {
            String choice = UtilView.listColumnOperate();
            switch (choice) {
                case "1":
                    ColumnController.insertColumn(currentUserDTO.getId());
                    break;
                case "2":
                    ColumnController.updateColumn(currentColumnDTOList);
                    break;
                case "3":
                    ColumnController.deleteColumn(currentColumnDTOList,loggedInuserDTO);
                    break;
                case "4":
                    columnOperate();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");

            }
        }
    }

    private static void searchColumn(){
        ColumnController.searchColumn();
        while (isLogIn && !currentColumnDTOList.isEmpty()) {
            String choice = UtilView.searchColumn();
            switch (choice) {
                case "1":
                    System.out.println("look at");
                    columnOperate();
                    break;
                case "2":
                    System.out.println("delete");
                    ColumnController.deleteColumn(currentColumnDTOList,loggedInuserDTO);
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }

    private static void columnOperate() {
        //登录，输入结果对，是本人的专栏才能继续
        while (isLogIn&&ColumnController.turnToColumn(currentColumnDTOList)&&currentColumnDTO.getUserId().equals(loggedInuserDTO.getId())) {
            String choice = UtilView.columnOperate();
                switch (choice) {
                    case "1":
                        ArticleController.addColumn(currentColumnDTO.getId(), currentUserDTO.getId());
                        break;
                    case "2":
                        ArticleController.removeColumn(currentArticleDTOList);
                        break;
                    case "3":
                        ArticleController.turnTop(currentArticleDTOList);
                        break;
                    case "4":
                        break;    
                    case "8":
                        return;
                    case "9":
                        isLogIn = false;
                        utilView.showMessage("已退出登录");
                        return;
                    case "0":
                        utilView.showMessage("已退出系统");
                        System.exit(0);
                    default:
                        utilView.showMessage("无效选项！");
                }
        }
    }

    private static void showSearchMenu() throws Exception {
        while (isLogIn) {
            String choice = UtilView.showSearchMenu();
            switch (choice) {
                case "1":
                    searchUser();
                    break;
                case "2":
                    ArticleController.searchArticle();
                    listArticleOperate();
                    break;
                case "3":
                    searchColumn();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }

    private static void searchUser() throws Exception {UserController.searchUser();
        while (isLogIn&&! currentUserDTOList.isEmpty()) {
            String choice = UtilView.searchUser();
            switch (choice) {
                case "1":
                    userOperate();
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }

    private static void userOperate() throws Exception {
        while (isLogIn&&UserController.turnToUser(currentUserDTOList)) {
            String choice = UtilView.userOperate();
            switch (choice) {
                case "1":
                    articleOperate();
                    break;
                case "2":
                    listColumnOperate();
                    break;
                case "3":
                    FollowController.turnFollowerId(loggedInuserDTO.getId(), currentUserDTO.getId());
                    break;
                case "8":
                    return;
                case "9":
                    isLogIn = false;
                    utilView.showMessage("已退出登录");
                    return;
                case "0":
                    utilView.showMessage("已退出系统");
                    System.exit(0);
                default:
                    utilView.showMessage("无效选项！");
            }
        }
    }



















}
